//
//  RegisterVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/12.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "RegisterVC.h"
#import "RegisterSuccessView.h"
#import "LoginResgisterModel.h"


@interface RegisterVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *emailTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UITextField *rePwdTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *signUpBtn;
@property (weak, nonatomic) IBOutlet UIButton *signinBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwdBtn;

@property (nonatomic, retain) RegisterSuccessView *successView;
@property (nonatomic, retain) UIView *popupView;

@property (nonatomic, retain) RefreshVM *vm;

@property (nonatomic,retain)NSMutableDictionary * param;

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.param = [NSMutableDictionary dictionary];
    [self setTFPlaceHolderTextColor:self.emailTF];
    [self setTFPlaceHolderTextColor:self.pwdTF];
    [self setTFPlaceHolderTextColor:self.rePwdTF];
    [self setTFPlaceHolderTextColor:self.codeTF];
}
-(void)setTFPlaceHolderTextColor:(UITextField *)tf{
    [tf setValue:DEF_0xColor(0x8F98E2, 1) forKeyPath:@"_placeholderLabel.textColor"];
    
}

- (IBAction)signUpBtnClick:(UIButton *)sender {
    [self setSuccessViewBlock];
}
- (IBAction)signInBtnClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



-(void)setSuccessViewBlock{
    
    [self.view addSubview:self.popupView];
    self.popupView.frame = CGRectMake(0, 0, screenW, screenH);
    
    [self.popupView addSubview:self.successView];
    weakself
    [self.successView setBtnClickBlock:^{
        
        if (self.emailTF.text.length<1) {
            
            [Utils showHudWithTitle:@"请输入账号" delay:1.0 target:ws];
            return;
        }
        if (self.pwdTF.text.length<1) {
            [Utils showHudWithTitle:@"请输入密码" delay:1.0 target:ws];

            return;
        }
        if (self.rePwdTF.text.length<1) {
            [Utils showHudWithTitle:@"请再次确认密码" delay:1.0 target:ws];

            return;
        }
        if (self.codeTF.text.length<1) {
            [Utils showHudWithTitle:@"请输入邀请码" delay:1.0 target:ws];

            return;
        }
        
        [ws configViewModel];
        
    }];
    
    self.successView.frame = CGRectMake(0, screenH, screenW, 290);
    
    [UIView animateWithDuration:0.6 animations:^{
        self.successView.frame = CGRectMake(0, screenH - 305, screenW, 290);
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    } completion:^(BOOL finished) {
        //        [self shakeAnimationForView:self.hView];
        self.successView.frame = CGRectMake(0, screenH - 290, screenW, 290);
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
            [self.view setNeedsLayout];
        }];
    }];
    
}

-(UIView *)popupView{
    if (!_popupView) {
        _popupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
        _popupView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    }
    return _popupView;
}
-(RegisterSuccessView *)successView{
    if (!_successView) {
        _successView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([RegisterSuccessView class]) owner:self options:nil].firstObject;
        
    }
    return _successView;
}


-(void)configViewModel{
    self.vm = [RefreshVM new];
    self.vm.urlStr = @"/api/users";
    self.vm.params = self.param;
    self.vm.dataClass = [LoginResgisterModel class];
    [self.vm requestDataNormal];
    weakself
    [self.vm setRequestResultBlock:^(id obj) {
//        LoginResgisterModel *model = obj;
        [Utils showHudWithTitle:@"注册成功" delay:2 completionBlock:^{
            [ws.navigationController popViewControllerAnimated:YES];
        }];
        
        
    }];
}


#pragma mark - textField Delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 1) {
        _param[@"account"] = textField.text;
    }
    if (textField.tag == 2) {
        _param[@"password"] = textField.text;
    }
    if (textField.tag == 3) {
        _param[@"password_confirmation"] = textField.text;
    }
    if (textField.tag == 4) {
        _param[@"icode"] = textField.text;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
